<?php

namespace app\api\controller;

use app\api\model\ConfigSystem as ConfigModel;
use think\Db;
use think\Request;

class Config extends Common
{
    public function appurl()
    {
        $config = ConfigModel::where(['category' => 'version'])->column('title, content');

        foreach ($config as $key => $item) {
            if (strpos($key, 'app') !== false) {
                $apple[$key] = $item;
            } else {
                $anzhuo[$key] = $item;
            }
        }

        return json([
            'code' => 200,
            'apple' => $apple,
            'anzhuo' => $anzhuo
        ]);
    }

    public function index(Request $request = null)
    {
        $init = $request->param('init');

        if ($init) {
            $contain = ['push', 'pshang'];

            $config = ConfigModel::where(['title' => ['in', $contain]])->column('title, content');
            $data = [
                'lianmai' => getenv('MIC_APPID'),
                'Bucket' => getenv('OSS_BUCKET'),
                'Endpoint' => getenv('OSS_ENDPOINT'),
                'request_scheme' => $_SERVER['REQUEST_SCHEME'] . '://',
                //'socket_prefix' => getenv('SOCKET_PREFIX'),
                //'socket_url' => getenv('SOCKET_PROTOCOL') . '://' . $_SERVER['SERVER_NAME'] . getenv('SOCKET_DIR') . ':' . getenv('SOCKET_PORT'),
            ];

            $data = array_merge($config, $data);
        } else {
            $contain = ['id', 'rate', 'anzhuourl', 'anzhuonum', 'adsw', 'appurl', 'appnum', 'applesw', 'recharge'];

            $data = ConfigModel::where(['title' => ['in', $contain]])->column('title, content');

            if (isset($data['id'])) {
                $data['id'] = (int)strip_tags($data['id']);
            }
        }

        return $this->pass($data);
    }

    public function env($param)
    {
        $contain = ['SHANYAN_APPID_ANDROID'];

        $param = explode(',', $param);

        $data = [];
        foreach ($param as $key => $item) {
            if (!in_array($item, $contain)) {
                unset($param[$key]);
            } else {
                $data[$item] = getenv($item);
            }
        }

        return $this->pass($data);
    }

    public function ranking()
    {
        $data = Db::name('config_ranking')
            ->field('types, name')
            ->where(['types' => ['in', [1,2,3,5,6,8]], 'is_hidden' => 0])
            ->order('sort desc, id asc')->select();

        return $this->pass($data);
    }
}
